import { defineStore } from 'pinia'

export const useGameStateStore = defineStore('gameState', {
  state: () => ({
    // 游戏场景状态
    currentScene: 'home', // home, map, battle, cultivation, etc.
    sceneData: {},
    
    // 背包信息（移除了玩家基础数据）
    inventory: {
      totalWeight: 45,
      maxWeight: 100,
      equipment: [
        { id: 1, name: '青云剑', type: '武器', level: 5, rarity: '仙品', description: '青云门传承宝剑，锋利无比', properties: { 攻击: '+50', 暴击率: '+5%' } },
        { id: 2, name: '护体符', type: '防具', level: 3, rarity: '上品', description: '增加防御力的符箓', properties: { 防御: '+30', 生命值: '+100' } },
        { id: 3, name: '破旧布衣', type: '防具', level: 1, rarity: '凡品', description: '普通布衣，提供微弱防护', properties: { 防御: '+5' } },
        { id: 4, name: '玄铁护手', type: '护具', level: 8, rarity: '灵品', description: '玄铁打造的护手，坚固异常', properties: { 防御: '+80', 格挡率: '+3%' } },
        { id: 5, name: '九转金丹', type: '丹药', level: 10, rarity: '神珍', description: '珍贵丹药，大幅提升修为', properties: { 修为: '+500' } }
      ],
      consumables: [
        { id: 6, name: '回血丹', type: '丹药', quantity: 5, rarity: '中品', description: '恢复50点生命值', properties: { 生命恢复: '+50' } },
        { id: 7, name: '聚气丹', type: '丹药', quantity: 3, rarity: '中品', description: '恢复30点法力值', properties: { 法力恢复: '+30' } },
        { id: 8, name: '大还丹', type: '丹药', quantity: 1, rarity: '上品', description: '全面恢复生命和法力', properties: { 生命恢复: '+200', 法力恢复: '+100' } }
      ],
      materials: [
        { id: 9, name: '灵草', type: '材料', quantity: 10, rarity: '凡品', description: '普通的修仙材料' },
        { id: 10, name: '灵石', type: '材料', quantity: 50, rarity: '中品', description: '蕴含灵气的石头' },
        { id: 11, name: '玄铁精', type: '材料', quantity: 3, rarity: '上品', description: '打造法宝的上等材料' },
        { id: 12, name: '九天玄火', type: '材料', quantity: 1, rarity: '仙品', description: '天地灵火，炼制神丹妙药的绝佳材料' }
      ]
    },
    
    // 成就系统
    achievements: {
      cultivation: [
        { name: '初入仙途', description: '成功筑基', completed: true, reward: '修为+100' },
        { name: '金丹大道', description: '凝结金丹', completed: false, reward: '修为+500' },
        { name: '元婴初成', description: '修炼至元婴期', completed: false, reward: '修为+1000' }
      ],
      exploration: [
        { name: '初探世界', description: '探索5个区域', completed: true, reward: '金币+100' },
        { name: '地图猎人', description: '探索所有区域', completed: false, reward: '修为+300' },
        { name: '秘境探险家', description: '探索3个秘境', completed: false, reward: '法宝碎片x5' }
      ],
      collection: [
        { name: '小有收获', description: '收集10件物品', completed: true, reward: '背包容量+10' },
        { name: '收藏家', description: '收集100件物品', completed: false, reward: '仓库容量+50' },
        { name: '功法大师', description: '收集所有功法', completed: false, reward: '随机功法x1' }
      ]
    },
    
    // 游戏设置
    settings: {
      autoCultivation: true,
      autoExplore: false,
      autoCombat: true,
      soundEnabled: true,
      musicVolume: 80,
      soundVolume: 100
    },
    
    // 游戏进度
    gameProgress: {
      storyChapter: 1,
      exploredAreas: ['青云山', '灵溪谷'],
      completedQuests: ['新手引导'],
      activeQuests: ['寻找灵草']
    }
  }),
  
  getters: {
    // 背包重量进度
    weightProgress: (state) => {
      return Math.round((state.inventory.totalWeight / state.inventory.maxWeight) * 100)
    },
    
    // 获取指定类型的物品
    getItemsByType: (state) => (type) => {
      switch(type) {
        case 'equipment':
          return state.inventory.equipment
        case 'consumables':
          return state.inventory.consumables
        case 'materials':
          return state.inventory.materials
        default:
          return []
      }
    },
    
    // 已完成成就数量
    completedAchievementsCount: (state) => {
      let count = 0
      Object.values(state.achievements).forEach(category => {
        count += category.filter(achievement => achievement.completed).length
      })
      return count
    }
  },
  
  actions: {
    // 场景切换
    changeScene(scene, data = {}) {
      this.currentScene = scene
      this.sceneData = data
    },
    
    // 背包操作
    addItemToInventory(item, type) {
      switch(type) {
        case 'equipment':
          this.inventory.equipment.push(item)
          break
        case 'consumables':
          const existingConsumable = this.inventory.consumables.find(i => i.name === item.name)
          if (existingConsumable) {
            existingConsumable.quantity += item.quantity
          } else {
            this.inventory.consumables.push(item)
          }
          break
        case 'materials':
          const existingMaterial = this.inventory.materials.find(i => i.name === item.name)
          if (existingMaterial) {
            existingMaterial.quantity += item.quantity
          } else {
            this.inventory.materials.push(item)
          }
          break
      }
      this.updateInventoryWeight()
    },
    
    removeItemFromInventory(itemId, type) {
      switch(type) {
        case 'equipment':
          this.inventory.equipment = this.inventory.equipment.filter(item => item.id !== itemId)
          break
        case 'consumables':
          this.inventory.consumables = this.inventory.consumables.filter(item => item.id !== itemId)
          break
        case 'materials':
          this.inventory.materials = this.inventory.materials.filter(item => item.id !== itemId)
          break
      }
      this.updateInventoryWeight()
    },
    
    updateInventoryWeight() {
      let weight = 0
      weight += this.inventory.equipment.reduce((sum, item) => sum + (item.weight || 1), 0)
      weight += this.inventory.consumables.reduce((sum, item) => sum + (item.weight || 0.5) * item.quantity, 0)
      weight += this.inventory.materials.reduce((sum, item) => sum + (item.weight || 0.2) * item.quantity, 0)
      
      this.inventory.totalWeight = Math.round(weight * 100) / 100
    },
    
    // 成就操作
    completeAchievement(category, achievementName) {
      const achievement = this.achievements[category].find(a => a.name === achievementName)
      if (achievement && !achievement.completed) {
        achievement.completed = true
        return achievement.reward
      }
      return null
    },
    
    // 设置更新
    updateSetting(key, value) {
      if (key in this.settings) {
        this.settings[key] = value
      }
    },
    
    // 游戏进度更新
    updateGameProgress(key, value) {
      if (key in this.gameProgress) {
        if (Array.isArray(this.gameProgress[key])) {
          this.gameProgress[key].push(value)
        } else {
          this.gameProgress[key] = value
        }
      }
    }
  }
})